package com.hellojava.util;

import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;

import java.util.UUID;
import java.util.regex.Pattern;

public class StringUtil {
    public static final Pattern EMAIL_PATTERN = Pattern.compile("^[\\w\\.\\-]+@[\\w\\.\\-]+(\\.[\\w\\-]{2,3}){1,2}$");

    public static final Pattern MOBILE_PATTERN = Pattern
            .compile("^((13[0-9])|(15[^4,\\D])|(17[0,6-8])|(18[0-9]))\\d{8}$");

    public static final char[] charMap;

    public static final String PREFIX_CODE = "^&^$%^$^FTFY233fs";

    public final static String DEFAULT_PWD = "ssmd123";

    static {
        charMap = new char[64];
        for (int i = 0; i < 10; i++) {
            charMap[i] = (char) ('0' + i);
        }
        for (int i = 10; i < 36; i++) {
            charMap[i] = (char) ('a' + i - 10);
        }
        for (int i = 36; i < 62; i++) {
            charMap[i] = (char) ('A' + i - 36);
        }
        charMap[62] = '_';
        charMap[63] = '-';
    }

    public static boolean isMobile(String input) {
        return MOBILE_PATTERN.matcher(input).matches();
    }

    public static String base64(String string) {
        return base64(string.getBytes());
    }

    public static String base64(byte[] bs) {
        return BaseEncoding.base64().encode(bs);
    }

    public static String sha1(String string) {
        return Hashing.sha1().hashString(string, Charsets.UTF_8).toString();
    }

    public static String md5(String string) {
        return Hashing.md5().hashString(string, Charsets.UTF_8).toString();
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String uuid(){
        return UUID.randomUUID().toString().replace("-","");
    }
    /**
     * 获取更短的UUID，长度为22
     */
    public static String get64UUID() {
        String uuid = "0" + getUUID();
        return hexTo64(uuid);
    }

    private static String hexTo64(String hex) {
        StringBuffer r = new StringBuffer();
        int index = 0;
        int[] buff = new int[3];
        int l = hex.length();
        for (int i = 0; i < l; i++) {
            index = i % 3;
            buff[index] = Integer.parseInt("" + hex.charAt(i), 16);
            if (index == 2) {
                r.append(charMap[buff[0] << 2 | buff[1] >>> 2]);
                r.append(charMap[(buff[1] & 3) << 4 | buff[2]]);
            }
        }
        return r.toString();
    }

    /**
     * 完整加密，含客户端加密及服务器端加密，即：md5(sha256)
     * 客户端加密为sha256加密
     *
     * @param s
     * @return
     */
    public static String completePassword(String s) {
        return serverSidePassword(Hashing.sha256().hashString(s, Charsets.UTF_8).toString());
    }

    /**
     * 仅服务器端加密，即：md5
     *
     * @param s
     * @return
     */
    // 外层加密，即md5，客户端请求使用该加密
    public static String serverSidePassword(String s) {
        return Hashing.md5().hashString(s, Charsets.UTF_8).toString();
    }

    /**
     * 获取随机数
     */
    public static String getRandomStr(int length) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            sb.append((int) (10 * Math.random()));
        }
        return sb.toString();
    }

    public static final String EMPTY = "";

    public static boolean isNil(String string) {
        return string == null || string.length() == 0;
    }

    public static boolean isNil(String... args) {
        for (String s: args) {
            if (isNil(s)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isNotNil(String string) {
        return !isNil(string);
    }
    public static boolean isNullOrEmpty(String str) {
        return null == str || 0 == str.trim().length();
    }


    /**
     * 密码加密
     * 首先进行SHA1 安全哈希算法，然后拼装加前缀码，最后进行MD5，即：md5(prefixCode+sha1)
     * @param s 需加密的字符串
     * @return 加密后的字符串
     */
    public static String encryptPassword(String s) {
        String secret = PREFIX_CODE + StringUtil.sha1(s);
        return StringUtil.md5(secret);
    }

    /**
     * 商户登录密码、支付密码 加密方式 md5(PREFIX_CODE+密码)
     * @param s
     * @return
     */
    public static String generatedStorePass(String s) {
        String secret = PREFIX_CODE + s;
        return md5(secret);
    }

    /**
     * 字符串转字符数组
     *
     * @param str
     * @param radix 多少进制
     * @return
     */
    public static byte[] string2bytes(String str, int radix) {
        byte[] srcBytes = str.getBytes();

        // 2个16进制字符占用1个字节，8个二进制字符占用1个字节
        int size = 2;
        if (radix == 2) {
            size = 8;
        }

        byte[] tgtBytes = new byte[srcBytes.length / size];
        for (int i = 0; i < srcBytes.length; i = i + size) {
            String tmp = new String(srcBytes, i, size);
            tgtBytes[i / size] = (byte) Integer.parseInt(tmp, radix);
        }
        return tgtBytes;
    }

    /**
     * string数组转换int数组
     * @param strArr
     * @return
     */
    public static int[] toIntArr(String... strArr){
        int[] intArr = new int[strArr.length];
        for(int i =0;i<strArr.length;i++){
            intArr[i] = Integer.parseInt(strArr[i]);
        }
        return intArr;
    }
}
